﻿Imports SQLUtil.SQL
Public Class frmUpdate
    Public oColumns As New Collection()
    Private Sub butCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCancel.Click
        Me.Close()
    End Sub

    Private Sub butOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butOk.Click
        If txtPrice.Text.Trim = "" Then
            txtPrice.Text = "0"
        End If

        If txtAgency_price.Text.Trim = "" Then
            txtAgency_price.Text = "0"
        End If
        frmMain.SaveFormDir(Me, txtId.Text.Trim)
    End Sub

    Private Sub frmUpdate_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtId.CharacterCasing = CharacterCasing.Upper
        ' set MaxLength cho cac Textbox tuong ung voi cac truong trong database
        Dim oLLK As New clsDirLookup.DirLibObj.DirLib(txtComponent_type_id, lblComponent_type_name, frmMain.appConn, "tbl_component_type", "id", "name", "COMPONENTTYPE", "1=1", False, butCancel)
        'Try
        Dim strSQL As String
        strSQL = "SELECT name, prec AS Maxlength FROM syscolumns "
        strSQL += "WHERE (id IN (SELECT id FROM sysobjects WHERE name = '" + clsDefine.cTable + "'))"
        Dim dsColumns As New DataSet
        Dim iColumn As Integer
        SQLRetrieve(frmMain.appConn, strSQL, "syscolumns", dsColumns)
        With dsColumns.Tables("syscolumns")
            For iColumn = 0 To .Rows.Count - 1
                oColumns.Add(.Rows(iColumn).Item("Maxlength"), Trim(.Rows(iColumn).Item("Name")))
            Next
        End With
        Dim iControl As Control
        For Each iControl In Me.Controls
            If Microsoft.VisualBasic.Left(iControl.Tag, 1) = "F" Then
                Dim iText As TextBox
                Dim rText As RichTextBox
                If TypeOf iControl Is RichTextBox Then
                    rText = iControl
                    Dim cFieldName = Microsoft.VisualBasic.Right(iControl.Name, iControl.Name.Length - 3)
                    If Mid(iControl.Tag, 2, 1) = "C" Then
                        ' Set MaxLength
                        rText = iControl
                        rText.MaxLength = oColumns(cFieldName)
                    End If
                Else
                    iText = iControl
                    Dim cFieldName = Microsoft.VisualBasic.Right(iControl.Name, iControl.Name.Length - 3)
                    If Mid(iControl.Tag, 2, 1) = "C" Then
                        ' Set MaxLength
                        iText = iControl
                        iText.MaxLength = oColumns(cFieldName)
                    End If
                End If                
                AddHandler iControl.KeyPress, AddressOf txtKeyPressEnter
            End If
        Next
        dsColumns = Nothing
        AddHandler txtAgency_price.KeyPress, AddressOf txtNumericKeyPress
        AddHandler txtPrice.KeyPress, AddressOf txtNumericKeyPress
        'Catch ex As Exception
        'End Try
    End Sub

    Public Sub txtKeyPressEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If AscW(e.KeyChar) = 13 Then
            SendKeys.Send("{Tab}")
        End If
    End Sub
    Public Sub txtNumericKeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        Dim tb As TextBox = CType(sender, TextBox)
        Dim chr As Char = e.KeyChar
        If IsNumeric(e.KeyChar) And Not e.KeyChar = "-" Then
            e.Handled = Not IsNumeric(tb.Text & e.KeyChar)
        ElseIf e.KeyChar = "." Then
            If Not (tb.SelectedText = "." Or IsNumeric(tb.Text & e.KeyChar)) Then
                e.Handled = True
            End If
        ElseIf e.KeyChar = "-" Then
            If tb.SelectionStart <> 0 Or Microsoft.VisualBasic.Left(tb.Text, 1) = "-" Then
                e.Handled = True
            End If
        ElseIf Not Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub
End Class